﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Text;

namespace MicroDal
{
    internal class DynamicDto : DynamicObject
    {
        private Dictionary<string, object> _fields = new Dictionary<string, object>(StringComparer.InvariantCultureIgnoreCase);
        public DynamicDto() { }

        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            if (!_fields.TryGetValue(binder.Name, out result))
                result = "n/a";
            return true;
        }

        public override bool TrySetMember(SetMemberBinder binder, object value)
        {
            _fields[binder.Name] = value;
            return true;
        }

        public override IEnumerable<string> GetDynamicMemberNames()
        {
            foreach (var key in _fields.Keys)
            {
                yield return key;
            }
        }

        internal void AddProperty(string propertyName, object propertyValue)
        {
            _fields.Add(propertyName, propertyValue);
        }
    }

}
